package Root;

import java.awt.AWTException;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.util.Hashtable;

import A002_Obrazek._A_DaneObrazka;

public class _B_Szukacz {
	
	private Robot rob;
	
	public _B_Szukacz() {
		try {
			rob = new Robot();
		} catch (AWTException e) {
			e.printStackTrace();
		}
	}

	public void FF (BufferedImage _ZrzutZewnetrzny, Hashtable <String, _A_DaneObrazka> obrazekTab){
		BufferedImage _zrzut;
		
		if (_ZrzutZewnetrzny == null){
			_zrzut = ZrobZrzutAwaryjny(RectangleFulSizeDisplay ());
		} else _zrzut = _ZrzutZewnetrzny;
		
			for (_A_DaneObrazka el : obrazekTab.values()){
				
				_A_Find(_zrzut, el);

			}
	
	}
	
	
	private void _A_Find( BufferedImage _zrzut, _A_DaneObrazka el){
		
		for (int yz = 0; yz < _zrzut.getHeight(); yz++){
			for (int xz = 0; xz < _zrzut.getWidth(); xz++){
				
				if ((xz - el.startX) + el.startX2 < _zrzut.getWidth() && yz + el.startY2 < _zrzut.getHeight()){
					
					if (_zrzut.getRGB(xz, yz) == el._PW && 
							_zrzut.getRGB((xz - el.startX) + el.startX2, yz + el.startY2) == el._OW){
						
						boolean test = szansa(_zrzut, xz - el.startX, yz, el);
						
						if (test == true) {
							//rob.mouseMove(xz, yz);
							try {
								Thread.sleep(100);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							System.out.print ("Znalazlem" + " " + el._NazwaObrazu +  " XY " + xz + ", " + yz + "\n");
						}
					}
				
				} 
				
			}
		}
		
	}
	
	private boolean szansa (BufferedImage _zrzut, int xz, int yz, _A_DaneObrazka el){
		int test = 0;
		BufferedImage obrazekTmp = el._filtrCrop;
		
		for (int yo = 0; yo < obrazekTmp.getHeight(); yo++){
			for (int xo = 0; xo < obrazekTmp.getWidth(); xo++){
				
				int xtmpz = xz + xo;
				int ytmpz = yz + yo;
				
				if (xtmpz < _zrzut.getWidth() && ytmpz < _zrzut.getHeight()){
						
					if ( obrazekTmp.getRGB(xo, yo) != -1 ){
						
						if (PW (_zrzut.getRGB(xtmpz, ytmpz), obrazekTmp.getRGB(xo, yo)) ){
							test ++;
	
						}// else return;
						
					}
					
				}
				
			}
		}
		
		
		if (test == el._sumaKontrolanPix) return true;
		else return false;
	}
	
	private boolean PW (int z, int o){
		
		int _tolerKoloru = 0;
		
	 	int red1 = (z >> 16) & 0xff;
	 	int green1 = (z >> 8) & 0xff;
	 	int blue1 = (z) & 0xff;
	 	
    	//int alpha2 = (Z >> 24) & 0xff;
	  	int red2 = (o >> 16) & 0xff;
	 	int green2 = (o >> 8) & 0xff;
	 	int blue2 = (o) & 0xff;
	 	
	 	if (((Math.abs(red1 - red2)) <= _tolerKoloru) && ((Math.abs(green1 - green2)) <= _tolerKoloru) && ((Math.abs(blue1 - blue2)) <= _tolerKoloru)){
	 		return true;
	 	} else return false;
		
		
		
	}
	
	
	
	private Rectangle RectangleFulSizeDisplay (){
		//System.out.println ("Brak wielkosci zrzutu - wykonam pelny ekran");
	    GraphicsEnvironment env = GraphicsEnvironment. getLocalGraphicsEnvironment(); 
	    GraphicsDevice devices = env.getDefaultScreenDevice();
	    int w = devices.getDisplayMode().getWidth();
	    int h = devices.getDisplayMode().getHeight();
        //System.out.println("Width "+devices.getDisplayMode().getWidth());
        //System.out.println("Height "+devices.getDisplayMode().getHeight()); 
	    return new Rectangle(0, 0, w, h);
	    //GraphicsDevice[] devices = env.getScreenDevices();
	}
	
	private BufferedImage ZrobZrzutAwaryjny(Rectangle rec){
		//System.out.println ("Brak obrazu zrzutu, wykonuje wlasny");
		BufferedImage buf = rob.createScreenCapture(rec);
		return buf;
	}
	
	
	
}
